package com.andyou.modules.sys.web;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.andyou.common.utils.CacheUtils;
import com.andyou.common.utils.DBbackupUtils;
import com.andyou.common.utils.StringUtils;
import com.andyou.common.web.BaseController;
import com.andyou.modules.sys.service.AppManageService;
import com.andyou.modules.sys.utils.UserUtils;

/**
 * 用户Controller
 * 
 * @author lgx
 * @version 2013-8-29
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/appManage")
public class AppManageController extends BaseController {

	@Autowired
	private AppManageService appManageService;

	/**
	 * 程序管理界面
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月20日；
	 */
	@RequiresPermissions("sys:appManage:appManageForm")
	@RequestMapping(value = "appManageForm")
	public String appManageForm(Model model, HttpServletResponse response) {
		return "modules/sys/appManageForm";
	}

	/**
	 * 运行更新脚本
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月20日；
	 */
	@RequiresPermissions("sys:appManage:runSql")
	@RequestMapping(value = "runSql")
	public String runSql(Model model, HttpServletResponse response) {

		Properties props;
		try {
			props = Resources.getResourceAsProperties("runSql.properties");
			String sqlFile = props.getProperty("sqlFile");
			String mess = appManageService.runSqlByReadFileContent(sqlFile);
			if (!StringUtils.isBlank(mess)) {
				addMessage(model, mess);
				return "modules/sys/appManageForm";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		addMessage(model, "执行脚本成功！");
		return "modules/sys/appManageForm";
	}

	/**
	 * 初始化模块
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月26日；
	 */
	@RequestMapping(value = "initModules")
	public String initModules(String sqlPath, Model model, HttpServletResponse response) {

		if (StringUtils.isBlank(sqlPath)) {
			addMessage(model, "请选择需要初始化的模块");
			return "modules/sys/appManageForm";
		}

		String mess = appManageService.runSqlByReadFileContent(sqlPath);
		if (!StringUtils.isBlank(mess)) {
			addMessage(model, mess);
			return "modules/sys/appManageForm";
		}
		addMessage(model, "初始化模块成功！");
		return "modules/sys/appManageForm";
	}

	/**
	 * 数据备份
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月20日；
	 */
	@RequiresPermissions("sys:appManage:backup")
	@RequestMapping(value = "backup")
	public String backup(Model model, HttpServletResponse response) {

		String backupPath = "";
		try {
			backupPath = DBbackupUtils.backup(null, null);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		addMessage(model, "数据库备份成功！所在服务器目录为:" + backupPath);
		return "modules/sys/appManageForm";
	}

	/**
	 * 清除组织架构缓存
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月21日；
	 */
	@RequestMapping(value = "clearCacheByOrg")
	public String clearCacheByOrg(Model model, HttpServletResponse response) {
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
		addMessage(model, "清除当前用户组织架构成功");
		return "modules/sys/appManageForm";
	}

	/**
	 * 清除菜单缓存
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月21日；
	 */
	@RequestMapping(value = "clearCacheByMenu")
	public String clearCacheByMenu(Model model, HttpServletResponse response) {
		UserUtils.removeCache(UserUtils.CACHE_MENU_LIST);
		addMessage(model, "清除当前用户菜单缓存成功");
		return "modules/sys/appManageForm";
	}

	/**
	 * 清除所有缓存
	 * 
	 * @param model
	 * @param response
	 * @return
	 * @author lgx Date:2017年8月21日；
	 */
	@RequestMapping(value = "clearAllCache")
	public String clearAllCache(Model model, HttpServletResponse response) {
		UserUtils.removeCache(UserUtils.CACHE_MENU_LIST);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
		CacheUtils.getCacheManager().clearAll();
		addMessage(model, "清除所有缓存成功");
		return "modules/sys/appManageForm";
	}
}
